Imports KanBan.Pgd.Infrastructure.Exceptions
Imports Iesi.Collections.Generic
Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Infrastructure.Repositories
Imports KanBan.Pgd.Presentation.Views

Public Class ManageGroupUsers

    Public Event UpdateUsersCount(ByVal groupId As Guid)

    Private Const DefaulGridViewRowHeight As Integer = 36

    Public Sub New(ByVal groupId As Guid)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        Me.GroupId = groupId

        Dim groupRepository As New GroupRepository()

        Dim users As ISet(Of User) = groupRepository.GetById(groupId).Users

        Me.AddUsers(users)

    End Sub

#Region "IManageGroupUsersView"

    Private _groupId As Guid
    Public Property GroupId() As Guid
        Get
            Return _groupId
        End Get
        Set(ByVal value As Guid)
            _groupId = value
        End Set
    End Property

    Public Sub AddUsers(ByVal users As IEnumerable(Of User))
        For Each u As User In users
            AddUser(u)
        Next
    End Sub

    Public Sub AddUser(ByVal user As User)
        Dim row As New DataGridViewRow()
        row.CreateCells(grvGroupUsers)
        row.Height = DefaulGridViewRowHeight

        row.Cells(1).Value = user.Id
        row.Cells(2).Value = String.Format("{0}", user.Name)

        grvGroupUsers.Rows.Add(row)
    End Sub

#End Region

    Private Sub grvGroupUsers_CellClick(ByVal sernder As Object, ByVal e As DataGridViewCellEventArgs) Handles grvGroupUsers.CellClick

        If e.RowIndex >= 0 Then

            Dim userId As Guid = CType(grvGroupUsers.Rows(e.RowIndex).Cells(GridColumns.Id).Value, Guid)

            If e.ColumnIndex = GridColumns.Remove Then
                If UIHelper.DeletionConfirmed("Are you sure you want to remove this User from the Group?") Then
                    Me.RemoveUserFromGroup(userId)
                    grvGroupUsers.Rows.Remove(grvGroupUsers.Rows(e.RowIndex))
                End If
            End If

        End If

    End Sub

    Public Sub RemoveUserFromGroup(ByVal userId As Guid)

        Dim groupRepository As New GroupRepository()
        Dim userRepository As New UserRepository()

        Dim groupId As Guid = Me.GroupId

        Dim group As Group = groupRepository.GetById(groupId)

        Dim userToRemove As User = userRepository.GetById(userId)

        group.RemoveUser(userToRemove)

        groupRepository.SaveOrUpdate(group)

    End Sub

    Private Sub btnAddUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddUser.Click
        Me.ShowAddUserDialog()
    End Sub

    Public Sub ShowAddUserDialog()

        Dim groupId As Guid = Me.GroupId

        Dim form As New AddUserDialog(groupId)

        AddHandler form.UserAddedToGroup, AddressOf OnUserAdded

        form.ShowDialog()

    End Sub

    Private Sub OnUserAdded(ByVal user As User)

        Me.AddUser(user)

    End Sub

    Private Enum GridColumns
        Image = 0
        Id = 1
        Name = 2
        Remove = 3
    End Enum

    Private Sub grvGroupUsers_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles grvGroupUsers.RowsAdded
        RaiseEvent UpdateUsersCount(Me.GroupId)
    End Sub

    Private Sub grvGroupUsers_RowsRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles grvGroupUsers.RowsRemoved
        RaiseEvent UpdateUsersCount(Me.GroupId)
    End Sub

End Class